<?php
class AppUsers {
var $id;
var $user_name;
var $pass_word;
var $full_name;
var $email;
var $mobile;
var $active_session;
var $locked;
var $active;
var $sms_alert_receiver;
var $social_media_login;
var $social_media_type;
var $login_success;
var $app_role_id;

    function open($username){
        global $db;
        $sql="select * from app_users where user_name='$username'";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
            $this->id=$id;
            $this->user_name=$user_name;
            $this->pass_word=$pass_word;
            $this->full_name=$full_name;
            $this->email=$email;
            $this->mobile=$mobile;
            $this->active_session=$active_session;
            $this->locked=$locked;
            $this->active=$active;
            $this->sms_alert_receiver=$sms_alert_receiver;
            $this->social_media_login=$social_media_login;
            $this->social_media_type=$social_media_type;
            $this->app_role_id=$app_role_id;
        }
    }

    function openAll(){
        $sql="select * from app_users order by id asc";
        $result=$db->query($sql);
        return $db->fetch_array($result);
        
    }

    function insert(){
    global $db;
    $sql="insert into app_users values (null,'{$this->user_name}',md5('{$this->pass_word}'),
'{$this->full_name}','{$this->email}','{$this->mobile}','{$this->active_session}',
'{$this->locked}','{$this->active}','{$this->sms_alert_receiver}',
'{$this->social_media_login}','{$this->social_media_type}',{$this->app_role_id})";
    $db->query($sql);
    return mysql_insert_id();
    }

    function update(){
        global $db;
        $sql="update app_users set user_name='{$this->user_name}',
full_name='{$this->full_name}',email='{$this->email}',mobile='{$this->mobile}',
active_session='{$this->active_session}',locked='{$this->locked}',active='{$this->active}',
sms_alert_receiver='{$this->sms_alert_receiver}',social_media_login='{$this->social_media_login}',
    social_media_type='{$this->social_media_type}',app_role_id={$this->app_role_id}
 where id=".$this->id;
        $db->query($sql);
    }
   function updatePassword($username,$password){
        global $db;
        $sql="update app_users set 
pass_word=md5('{$this->pass_word}') where user_name='$username'";
        $db->query($sql);
    }
    function delete(){
        global $db;
        $sql="delete from app_users where id=".$this->id;
        $db->query($sql);
    }
    function loginUser($password){
        global $db;
         
         
        if(strcmp($this->pass_word,md5($password))==0){
                $this->login_success="succeed";
            } else {
               $this->login_success="fail"; 
            }
             
       return $this->login_success;     
           
    }
    function getLoginStatus(){
        return $this->login_success;
    }
    function updateSession($session){
        global $db;
        $sql="update app_users set active_session='$session' where id=".$this->id;
        $db->query($sql);
    }
    function getRole(){
        global $db;
        $sql="select role_name from app_roles where id={$this->app_role_id}";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            return $row['role_name'];
        
        } else return "";
    }
    function is_exist($username){
    global $db;
    $sql="select count(*) from app_users where user_name='$username'";
    $row=$db->fetch_row($sql);
    if (!empty($row[0])){
       // echo "jumlah user".$row[0];
        return $row[0]>0;
    } else return false;
}
    
}

?>
